package com.budwk.app.sqfwV2.generator.centuryCity;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.StrFormatter;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.PdfGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.BasePdfGenerator;
import com.budwk.app.sqfwV2.dto.centuryCity.CenturyCityOldCarePdfDTO;
import com.budwk.app.sqfwV2.dto.centuryCity.CenturyCityProblemParamDTO;
import com.budwk.app.sqfwV2.enums.HiddenDangerEnum;
import com.budwk.app.sqfwV2.enums.centuryCityOldCare.CenturyCityOldCareV2FileEnums;
import com.budwk.app.util.poiUtils.CommonFormOfficeUtils;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * @author 东方祥云低代码1.1
 * @since 2024-06-06
 * <p>
 * 世纪城养老服务站巡查1.1 CenturyCityOldCare
 * 养老机构安全生产检查表.docx
 */
public class FoodServiceDailyInsPdfGenerator extends BasePdfGenerator {

    private final Map<String, HiddenDangerEnumBO> hiddenDangerEnumBOMap;

    public FoodServiceDailyInsPdfGenerator() {
        Map<String, HiddenDangerEnumBO> hiddenDangerEnumTempMap = Maps.newHashMap();
        for (HiddenDangerEnum value : HiddenDangerEnum.values()) {
            HiddenDangerEnumBO hiddenDangerEnumBO = new HiddenDangerEnumBO();
            BeanUtil.copyProperties(value, hiddenDangerEnumBO);
            hiddenDangerEnumTempMap.put(value.getCode(), hiddenDangerEnumBO);
        }
        this.hiddenDangerEnumBOMap = hiddenDangerEnumTempMap;
    }

    /**
     * 判断使用使用这个generator生成pdf
     *
     * @param pdfDTO 创建pdf数据素材
     * @return 结果
     */
    @Override
    public boolean pdfCreateConditionCheck(PdfGenerateParam pdfDTO) {
        return true;
    }

    @Override
    public String getTemplatePath(PdfGenerateParam pdfGenerateParam) {
        return CenturyCityOldCareV2FileEnums.foodServiceDailyInsPdf.getPath();
    }

    @Override
    public void customerPopularPdfParam(Map<String, Object> paramMap, PdfGenerateParam pdfGenerateParam) {
        CenturyCityOldCarePdfDTO paramData = (CenturyCityOldCarePdfDTO) pdfGenerateParam.getAbstractCustomerPdfCreateField();

        paramMap.put("fillDateStr", DateUtil.format(pdfGenerateParam.getBusinessParam().getFillDate(), "yyyy年MM月dd日"));

        Date deadlineForRectification = paramData.getDeadlineForRectification();
        if (deadlineForRectification != null) {
            Date fillDate = pdfGenerateParam.getBusinessParam().getFillDate();
            long dayBetween = DateUtil.betweenDay(fillDate, deadlineForRectification, true);
            paramMap.put("deadlineForRectification", dayBetween);
        }

        paramMap.put("transferToMarketSupervisionAndManagement", paramData.getTransferToMarketSupervisionAndManagement());

        String numberOfEmployees = "";
        String numberOfValidHealthCertificates = "";

        List<String> riskImageList = Lists.newArrayList();
        List<String> rectificationAfterImageList = Lists.newArrayList();
        Set<String> statisticProblemRiskCodeSet = Sets.newHashSet(
                "83", "84", "85", "86", "50", "52", "51", "87"
                , "48", "49", "54", "53", "88", "89", "90"
        );  // 用于统计这几个问题的问题照片

        // 使用这种方式快速的把问题和问题对应的值赋值进来
        List<CenturyCityProblemParamDTO> foodServiceFormList = paramData.getFoodServiceForm();
        if (CollectionUtil.isNotEmpty(paramData.getFoodServiceForm())) {
            for (CenturyCityProblemParamDTO foodServiceItem : foodServiceFormList) {
                String riskCode = foodServiceItem.getRiskCode();
                String key = foodServiceItem.getKey();
                HiddenDangerEnumBO hiddenDangerEnumBO = this.hiddenDangerEnumBOMap.get(riskCode);
                if (hiddenDangerEnumBO != null) {
                    hiddenDangerEnumBO.setKey(key);
                    this.hiddenDangerEnumBOMap.put(riskCode, hiddenDangerEnumBO);
                }

                if (statisticProblemRiskCodeSet.contains(riskCode) && StringUtils.isNotBlank(foodServiceItem.getRiskImages())) {
                    riskImageList.addAll(Lists.newArrayList(Splitter.on(",").split(foodServiceItem.getRiskImages())));
                }
                if (statisticProblemRiskCodeSet.contains(riskCode) && StringUtils.isNotBlank(foodServiceItem.getRectificationAfterPhoto())) {
                    rectificationAfterImageList.addAll(Lists.newArrayList(Splitter.on(",").split(foodServiceItem.getRectificationAfterPhoto())));
                }

                if (StringUtils.isNotBlank(foodServiceItem.getNumberOfEmployees())) {
                    numberOfEmployees = foodServiceItem.getNumberOfEmployees();
                }

                if (StringUtils.isNotBlank(foodServiceItem.getNumberOfValidHealthCertificates())) {
                    numberOfValidHealthCertificates = foodServiceItem.getNumberOfValidHealthCertificates();
                }
            }
        }

        // 下面是各个服务问题的汇总 接下来的代码就比较魔幻了，请参考这个在线文档，我的天我写的时候我也是崩溃的
        // https://docs.qq.com/sheet/DZGVVQ2lpQXJibWxX?tab=kcuch4
        String ser1 = "是", ser2 = "是", ser3 = "否", ser4 = "是", ser5 = "是", ser6 = "是";
        String ser7 = "是", ser8 = "是", ser9 = "是", ser10 = "是", ser11 = "是", ser12 = "是";

        ser1 = Optional.ofNullable(this.hiddenDangerEnumBOMap.get("83").getKey()).orElse(ser1);
        ser2 = Optional.ofNullable(this.hiddenDangerEnumBOMap.get("84").getKey()).orElse(ser2);
        ser3 = Optional.ofNullable(this.hiddenDangerEnumBOMap.get("85").getKey()).orElse(ser3);
        ser4 = Optional.ofNullable(this.hiddenDangerEnumBOMap.get("86").getKey()).orElse(ser4);
        ser5 = Optional.of(this.hiddenDangerEnumBOMap.get("50").getKey().equals("是") &&
                this.hiddenDangerEnumBOMap.get("52").getKey().equals("是") ? "是" : "否").orElse(ser5);
        ser6 = Optional.of(this.hiddenDangerEnumBOMap.get("51").getKey().equals("是") &&
                this.hiddenDangerEnumBOMap.get("87").getKey().equals("是") ? "是" : "否").orElse(ser6);
        ser7 = Optional.of(this.hiddenDangerEnumBOMap.get("48").getKey().equals("是") &&
                this.hiddenDangerEnumBOMap.get("49").getKey().equals("是") ? "是" : "否").orElse(ser7);
        ser8 = Optional.ofNullable(this.hiddenDangerEnumBOMap.get("54").getKey()).orElse(ser8);
        ser9 = Optional.ofNullable(this.hiddenDangerEnumBOMap.get("53").getKey()).orElse(ser9);
        ser10 = Optional.ofNullable(this.hiddenDangerEnumBOMap.get("88").getKey()).orElse(ser10);
        ser11 = Optional.ofNullable(this.hiddenDangerEnumBOMap.get("89").getKey()).orElse(ser11);
        ser12 = Optional.ofNullable(this.hiddenDangerEnumBOMap.get("90").getKey()).orElse(ser12);

        paramMap.put("noe", numberOfEmployees);
        paramMap.put("novhc", numberOfValidHealthCertificates);
        paramMap.put("ser1", ser1);
        paramMap.put("ser2", ser2);
        paramMap.put("ser3", ser3);
        paramMap.put("ser4", ser4);
        paramMap.put("ser5", ser5);
        paramMap.put("ser6", ser6);
        paramMap.put("ser7", ser7);
        paramMap.put("ser8", ser8);
        paramMap.put("ser9", ser9);
        paramMap.put("ser10", ser10);
        paramMap.put("ser11", ser11);
        paramMap.put("ser12", ser12);

        String imagesDesc = "", imagesLastDesc = "";
        if (riskImageList.size() > 0) {
            imagesDesc = StrFormatter.format("{} {}餐饮服务日常巡查整改前照片", paramData.getPlaceName(),
                    DateUtil.format(paramData.getInspectionDate(), "yyyy年MM月dd日"));
        }
        paramMap.put("imagesDesc", imagesDesc);
        paramMap.put("images", CommonFormOfficeUtils.addWordPicture(riskImageList, "pictures", 5f));


        if (rectificationAfterImageList.size() > 0) {
            imagesLastDesc = StrFormatter.format("{} {} 整改后照片", paramData.getPlaceName(),
                    DateUtil.format(paramData.getInspectionDate(), "yyyy年MM月dd日"));
        }

        paramMap.put("imagesLastDesc", imagesLastDesc);
        paramMap.put("imagesLast", CommonFormOfficeUtils.addWordPicture(rectificationAfterImageList, "pictures", 5f));
    }
}
